<?php
class theme{

	/**
	 * zwraca domyslny numer strony
	 * na podstawie kolumny default = 1
	 *
	 * @return Int
	 */
	public static function _get_default_page_info($__index,$__lang=null){
		global $config;
		if (is_null($__lang)) $__lang = $config->__lang;
		$__q_default_page_column = "SELECT a.{$__index} FROM ".PAGES." a,".PLANG." b  
			WHERE b.id_page = a.id_page AND  a.defaultp = 1 AND b.id_lang = ".tools::__mres($__lang);
		$__result  = tools::__adodb('getOne',$__q_default_page_column);
		return $__result;
	}
	
	/**
	 * numer id wybranej strony 
	 *
	 * @param Str $__page
	 * @return Int
	 */
	public static function _get_page_id($__page){
		global $config;
		$__q_page_id = "SELECT a.id_page FROM ".PAGES." a,".PLANG." b  
			WHERE b.id_page = a.id_page 
			AND b.page_url = '{$__page}'
			AND b.id_lang = ".tools::__mres($config->__lang);
		$__id_page = tools::__adodb('getOne',$__q_page_id);
		if ($__page == "install") return 0;
		//jesli nie ma strony sprawdz w innej wersji jezykowej
		if(empty($__id_page)){
			$__q_page_id = "SELECT b.id_page FROM  ".PLANG." b WHERE b.page_url = '{$__page}' LIMIT 1";
			$__id_page = tools::__adodb('getOne',$__q_page_id);
			
			$__q_page_id = "SELECT a.id_page, b.page_url FROM ".PAGES." a,".PLANG." b  
				WHERE b.id_page = a.id_page 
				AND a.id_page = '{$__id_page}'
				AND b.id_lang = ".tools::__mres($config->__lang);
			$__id_page = tools::__adodb('getRow',$__q_page_id);			
		}
		return $__id_page;
	}


	/**
	 * zwraca wartosc kolumny z tabeli z informacjami o stronie
	 *
	 * @param String $__atrib
	 * @param String $__lang
	 * @param Number $__id_page
	 * @return String
	 */
	public static function _get_page_atribut($__atrib=null,$__lang=null,$__id_page){
		global $config;
		if (is_null($__lang)) $__lang = $config->__lang;
		$__q_page_column = "SELECT {$__atrib} FROM ".PAGES." a,".PLANG." b  
			WHERE b.id_page = a.id_page AND  a.id_page = '{$__id_page}' AND b.id_lang = '$__lang'";
		return tools::__adodb('getOne',$__q_page_column);		
	}
	
	/**
	 * zwraca liste sekcji danej strony
	 *
	 * @param Int_type $__id_page
	 * @return Arr
	 */
	private static function _get_data_section_page($__id_page){
		global $config;
		$__q_data_page = "SELECT id_page, id_section FROM ".THEME." 
		 	WHERE  id_page = '{$__id_page}' AND id_lang = '{$config->__lang}'
		 	AND plock != 1 ";
		return tools::__adodb('getAll',$__q_data_page);
	}
	
	/**
	 * zwraca numer stylu jak ma zostac wykozystany do budowy strony
	 *
	 * @param Int $__id_page
	 * @return Int
	 */
	private static function _get_data_theme_info($__id_page){
		$__q_theme_info = "SELECT theme FROM ".PAGES." a 
		 	WHERE a.id_page = '{$__id_page}' ";
		$__theme = tools::__adodb('getOne',$__q_theme_info);
		if (empty($__theme)) return 1;
		else return $__theme;
	}
	
	

	/**
	 * zwraca jaki zostanie uzyty styl strony
	 *
	 * @return Int
	 */
	public static function get_theme_info($__id_page){
		if ( is_numeric($__id_page) && $__id_page > 0 ){
			return theme::_get_data_theme_info($__id_page);
		}else{
			return theme::_get_data_theme_info(0);
		}		
	}
	
	/**
	 * zwraca wszystkie sekcje danej strony
	 *
	 * @return unknown
	 */
	public static function get_section_info($__id_page){
			if ( is_numeric($__id_page) && $__id_page > 0 ){
				return theme::_get_data_section_page($__id_page);
			}else{
				return array(array('id_page'=>0,'id_section'=>1));
			}
	}

	/**
	 * zwraca numer strony do wygenerowania
	 *
	 * @return Int
	 */
	public static function _define_page(){
		global $config;
		$__page = (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) ? $_REQUEST['page'] : null;		
		$__id_page__ = theme::_get_page_id($__page);
		$__id_page  = (is_array($__id_page__))? $__id_page__[0]:$__id_page__;
		$__page  = (is_array($__id_page__))? $__id_page__[1]:$config->page;
		
		if (empty($__id_page)) $__id_page = theme::_get_default_page_info('id_page',$config->__lang);
		if(is_null($__page) || empty($__page)) {
			$__page = theme::_get_page_atribut('page_url',null,$__id_page);
			//tools::_redirect("{$__page}.html",'',0,'php');
			$config->setParam('page',theme::_get_page_atribut('page_url',null,$__id_page));
		}		
		
		tools::_assign("page",$__page);			
		return $__id_page;
	}
	
	
	/**
	 * 
	 * @return unknown_type
	 */
	public static function saveSettings($settings=null,$db=null){
		global $config;
		$settings_ = (is_null($settings)) ? $config->settings : $settings;
		foreach ($settings_ as $id => $wartosc) {
			$id__ = tools::__adodb('getOne',"SELECT id  FROM ".SETUP." WHERE id = ".tools::__mres($id),true,$db); 
			if($id == $id__){
				tools::__adodb("exec","UPDATE ".SETUP." SET wartosc = ".tools::__mres($wartosc)." WHERE id = ".tools::__mres($id),true,$db);
			}else{
				tools::__adodb("exec","INSERT INTO ".SETUP." (id, wartosc) VALUES (".tools::__mres($id).",".tools::__mres($wartosc).");",true,$db);
			}
		}
	}
	
	public static function resetujUstawienia(){
		$theme = tools::__adodb('getOne',"SELECT wartosc  FROM ".SETUP." WHERE id = 'bierzacy_schemat'"); 
		tools::__adodb('exec',"DELETE FROM ".SETUP." WHERE id NOT IN ('css','szablon','ramka_theme','ramka_theme_kolor')"); 
	}


}
?>